$(function() {

	$('#li_sys').addClass('active open');
	$('#svconfig').addClass('active');

	$("#connecttest").click(function() {
		var ip=$("#serverip").val();
		var svtype=$("#svtype").val();
		var username=$("#username").val();
		var password=$("#password").val();
		var contype=$("#contype").val();
		
		$.ajax({
			type : 'post',
			url : 'svconfig/test',
			dataType : 'text',					
			data : {
				"ip":ip,
				"svtype":svtype,
				"username" : username,
				"password" : password,
				"contype":contype
			},
			success : function(msg) {
				console.log(msg);
				if(msg=="0"){
					alert("连接成功");
				}else{
					alert("连接失败");
				}
			},
			error : function(msg) {
				console.log('failed')
			}
		});    			
	});
	
	$("#saveconfig").click(function() {
		var sname=$("#servername").val();
		var ip=$("#serverip").val();
		var svtype=$("#svtype").val();
		var username=$("#username").val();
		var password=$("#password").val();
		var contype=$("#contype").val();
		
		$.ajax({
			type : 'post',
			url : 'svconfig/insert',
			dataType : 'text',					
			data : {
				"sname":sname,
				"ip":ip,
				"svtype":svtype,
				"username" : username,
				"password" : password,
				"contype":contype
			},
			success : function(msg) {
				console.log(msg);		
				if(msg=="1"){
					alert("录入成功");
					
					$("#servername").val('');
					$("#serverip").val('');
					$("#svtype").val('');
					$("#username").val('');
					$("#password").val('');
					$("#contype").val('');
					
				}else{
					alert("录入失败");
				}
			},
			error : function(msg) {
				console.log('failed')
			}
		});    
		
	});

	//when server type is selected as window, hide connect type
	$("#svtype").change(function(){
		  var selected=$("#svtype").val();
		  //console.log(selected);		  
		  
		  if(selected=='Linux'){
			  $("#con-div").show();
		  }else if(selected='Windows'){
			  $("#con-div").hide(); 
		  }
	});
	
});